package com.googlecode.datawander;


import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.ObjectServer;
import com.db4o.ObjectSet;
import com.db4o.config.Configuration;
import com.db4o.query.Query;
import com.db4o.reflect.generic.GenericClass;
import com.db4o.reflect.generic.GenericReflector;
import com.db4o.reflect.jdk.JdkReflector;
import java.math.BigDecimal;
/**
 * Version for db4o 7.4
 * @author MarcinStachniuk
 */
public class Db4oUtilTestPckg {
    private static String databaseFilename = "db4o.yap";
    private static ObjectServer objectServer;
    private static final ThreadLocal<ObjectContainer> dbThreadLocal = new ThreadLocal<ObjectContainer>();

    public static ObjectContainer getObjectContainer() {
        ObjectContainer oc = (ObjectContainer) dbThreadLocal.get();
        if (oc == null || oc.ext().isClosed()) {
            Configuration conf = Db4o.newConfiguration();
//            conf.objectClass(PHOTO_GALLERY.class).cascadeOnUpdate(true);
//            conf.objectClass(PHOTO.class).cascadeOnUpdate(true);
//            conf.objectClass(PHOTOS.class).cascadeOnUpdate(true);
            // save BigDecimal in db4o 7.4
            conf.objectClass(BigDecimal.class).callConstructor(true);
            conf.objectClass(BigDecimal.class).storeTransientFields(true);
            oc = Db4o.openFile(conf, databaseFilename);
            dbThreadLocal.set(oc);
        }
        return oc;
    }
    public static void closeObjectContainer() {
        ObjectContainer oc = (ObjectContainer) dbThreadLocal.get();
        dbThreadLocal.set(null);
        if (oc != null) oc.close();
    }
    public static void shutdown() {
        if (objectServer != null) {
            objectServer.close();
        }
    }
    public static String getDatabaseFilename() {
        return databaseFilename;
    }
    public static void setDatabaseFilename(String databaseFilename) {
        Db4oUtilTestPckg.databaseFilename = databaseFilename;
    }

    public static int getNumberOfObjectsForClass(String className) {
        GenericClass cl = makeGenericClass(className);

        ObjectContainer db = Db4oUtilTestPckg.getObjectContainer();

        Query query = db.query();
        query.constrain(cl);

        ObjectSet<Object> objSet = query.execute();

        return objSet.size();
    }

    public static GenericClass makeGenericClass(String className) {
            GenericReflector reflector = getReflector();
            GenericClass _objectIClass = (GenericClass) reflector
                            .forClass(Object.class);
            GenericClass result = new GenericClass(reflector, null, className, _objectIClass);
            return result;
    }

    public static GenericReflector getReflector() {
            GenericReflector reflector = new GenericReflector(null,
                            new JdkReflector(Thread.currentThread().getContextClassLoader()));
            return reflector;
    }
}
